package junk;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;

import com.sun.tools.javac.util.List;

import processing.core.PApplet;

class ReadGenes
{
	public static String[] datasets = {"Control", "HP", "MD", "HOSVD","OriginHOSVD", "ProteinControl", "ProteinH202", "ProteinBig","Translatome"};
	public static String[] filenames = {"partial_genes4329.txt","partial_genes4329HP.txt", "partial_genes4329MD.txt","HOSVD_4329.txt","OriginHOSVD.txt","proteinBinding4329.txt", "proteinBindingH202.txt", "lotOfProteinPValues.txt","translatome.txt"};
	public static String[] dataType = {"Raw", "Raw", "Raw", "Raw","Raw", "P-Value","P-Value","P-Value","Raw"};
	public static int[] numArrays = {7,7,7,7,7,23,66,203,12};
	public static int[] numAnnotations = {10,10,10,10,16,10,1,1,1};
	ReadGenes( PApplet p)
	{
		String[][] file = new String[datasets.length][];
		String[][][] annotations = new String[datasets.length][][];
		String[][] names = new String[datasets.length][];
		float[][][] expression = new float[datasets.length][][];
		ArrayList <String> proteinAnnotations = new ArrayList <String>();
		ArrayList <String> proteinNames = new ArrayList <String>();
		Gene[] geneList;
		int sum = 0;
		for(int i = 0; i< datasets.length; i++) sum += numArrays[i];
		Protein[] proteinList = new Protein[sum];
		String[] namesList = null;
		
		Set<String> s  = new HashSet<String>();
		for(int z = 0; z< datasets.length; z++)
		{			
			file[z] = p.loadStrings(filenames[z]);
			
			proteinAnnotations.add(file[z][0]);
			proteinNames.add(file[z][1]);
			String[] tmp = new String[file[z].length-2];
			for(int i = 2; i< file[z].length; i++)
				tmp[i-2] = file[z][i];
			file[z] = tmp;
			Arrays.sort(file[z]);

			annotations[z]= new String[file[z].length][numAnnotations[z]];
			expression[z] = new float[file[z].length][numArrays[z]];
			
			names[z] = new String[file[z].length];
			int j = 0;
			
			while (j< file[z].length)
			{
				StringTokenizer st = new StringTokenizer( file[z][j] );
				for( int i = 0; i< numAnnotations[z]; i++)
					annotations[z][j][i]= st.nextToken();
				//st.nextToken();
				for( int i = 0; i< numArrays[z]; i++)
					expression[z][j][i] = Float.parseFloat(st.nextToken());
				names[z][j] = annotations[z][j][0];
				j++;
			}
			//Arrays.sort(names[z]);
			java.util.List<String> list = Arrays.asList(names[z]);
			s.addAll(list);
			namesList = s.toArray( new String[0]);
		}
		Arrays.sort(namesList);
		for(int z = 0; z< datasets.length; z++)
		{	
			Arrays.sort(names[z]);
			for(String name: namesList)
			{
				int i = 0;
				String name2;
				while( i< names[z].length)
				{
					name2 = names[z][i];
					if(name == name2) 
					{
						i ++;
						break;
					}
					else if (name.equals(name2))
					{
						annotations[z][i][0] = name;
						i ++; 
						break;
					}
					else i ++;
				}
			}
		}
		Arrays.sort(namesList);
		geneList = new Gene[namesList.length];
		for( int i = 0; i< namesList.length; i++)
		{
			boolean pflag = true;
			geneList[i] = new Gene(p);
			for(int z = 0; z< datasets.length; z++)
			{
				boolean flag = true;
				for(int j = 0; j< annotations[z].length; j++)
				{
					//if(annotations[z][j][0].equals(namesList[i]))
					if(annotations[z][j][0] == namesList[i])
					{

						flag = false;
						if(geneList[i].annotations.size() == 0)	
						{
							for(int f= 0; f< 26; f++) geneList[i].annotations.add("None");
						}
						if(numAnnotations[z] == 16 && geneList[i].annotations.get(10).equals("None"))	
						{
							int m = 10;
							for( String str: annotations[z][j]) 
							{
								geneList[i].annotations.set(m++, str);
							}
							geneList[i].name = annotations[z][j][0];
						}
						else if (geneList[i].annotations.get(0).equals("None"))
						{
							int m = 0;
							for( String str: annotations[z][j]) 
							{
								geneList[i].annotations.set(m++, str);
							}
							geneList[i].name = annotations[z][j][0];
						}
						else if(numAnnotations[z] == 1)
						{
							geneList[i].name = annotations[z][j][0];
						}
						geneList[i].addExpression(expression[z][j]);				
						
						break;
					}
				}
				if(flag)
				{
					float[] noData = new float[numArrays[z]];
					if(dataType[z].equals("P-Value")) Arrays.fill(noData, 1f);
					else Arrays.fill(noData, 0f);
					geneList[i].addExpression(noData);			
				}
			}
		}
		
		String name, annotation;
		int count = 0;
		int proteinCount = 0;
		for(int z = 0; z< datasets.length; z++)
	{
			StringTokenizer st3 = new StringTokenizer( proteinNames.get(proteinCount) );
			StringTokenizer st4 = new StringTokenizer( proteinAnnotations.get(proteinCount) );
			proteinCount++;
			for (int i = 0; i< numAnnotations[z]; i++)
			{
				st3.nextToken();
				st4.nextToken();
			}
			for(int i = 0; i< numArrays[z]; i++)
			{
				name = st3.nextToken();
				annotation = st4.nextToken();
				proteinList[count++] = new Protein( dataType[z], name, annotation, datasets[z], i, p);	
			}
		}				
		Gene.geneList = geneList;
	
		
		
		Protein[] groupList = new Protein[1 + Main1.proteinGroups.length];
		groupList[0] = new Protein("Group", "All Groups","None", p);
		int numProteins = 0;
		for(int i = 0; i< numArrays.length; i++)
			numProteins += numArrays[i];
		
		for(int i = 0; i < Main1.proteinGroups.length; i++)
		{
			groupList[i+1] = new Protein("Group", Main1.proteinGroups[i], "Groups", p);
			groupList[0].children.add(groupList[i+1]);
			
			for(Protein protein: proteinList)
			{
				if( protein.annotation.equals(groupList[i+1].name))
				{
					groupList[i+1].children.add(protein);
					protein.parentProtein = groupList[i+1];
				}
			}			
		}
		Protein[] proteinList2 = new Protein[proteinList.length + groupList.length];
		for(int i  = 0; i< proteinList.length; i++) proteinList2[i] = proteinList[i];
		for(int i  = 0; i< groupList.length; i++) proteinList2[i+ proteinList.length] = groupList[i];
		Protein.proteinList = proteinList2;
		Protein.allProteins = groupList[0];
	
	}
}